package nmea0183

import (
	"github.com/project-flogo/core/data/coerce"
)

type Settings struct {
	SkipVerify bool `md:"skipVerify"`
}

type Input struct {
	Sentence string `md:"sentence" json:"sentence,omitempty"`
	Fields   string `md:"fields"`
	Type     string `md:"type"`
}

func (r *Input) ToMap() map[string]interface{} {
	return map[string]interface{}{
		"sentence": r.Sentence,
		"fields":   r.Fields,
		"type":     r.Type,
	}
}

func (r *Input) FromMap(values map[string]interface{}) error {
	sentence, err := coerce.ToString(values["sentence"])
	if err != nil {
		return err
	}
	r.Sentence = sentence

	fields, err := coerce.ToString(values["fields"])
	if err != nil {
		return err
	}
	r.Fields = fields

	t, err := coerce.ToString(values["type"])
	if err != nil {
		return err
	}
	r.Type = t

	return nil
}

type Output struct {
	Data map[string]interface{} `md:"data" json:"data,omitempty"`
	Err  string                 `md:"err"`
}

func (r *Output) ToMap() map[string]interface{} {
	return map[string]interface{}{
		"data": r.Data,
		"err":  r.Err,
	}
}

func (r *Output) FromMap(values map[string]interface{}) error {
	data, err := coerce.ToObject(values["data"])
	if err != nil {
		return err
	}
	r.Data = data

	errStr, err := coerce.ToString(values["err"])
	if err != nil {
		return err
	}
	r.Err = errStr

	return nil
}
